﻿@inject Microsoft.Extensions.Localization.IStringLocalizer<AddEditDocumentModal> _localizer

<EditForm Model="@AddEditDocumentModel" OnValidSubmit="SaveAsync">
    <FluentValidationValidator @ref="_fluentValidationValidator" />
    <MudDialog>
        <TitleContent>
            @{
                if (AddEditDocumentModel.Id == 0)
                {
                    <MudText Typo="Typo.h6">
                        <MudIcon Icon="@Icons.Material.Filled.Add" Class="mr-3 mb-n1" />
                        @_localizer["Add Document"]
                    </MudText>
                }
                else
                {
                    <MudText Typo="Typo.h6">
                        <MudIcon Icon="@Icons.Material.Filled.Update" Class="mr-3 mb-n1" />
                        @_localizer["Update Document"]
                    </MudText>
                }
            }
        </TitleContent>
        <DialogContent>
            <MudGrid>
                @if (AddEditDocumentModel.Id != 0)
                {
                    <MudItem xs="12" md="6">
                        <MudTextField Disabled For="@(() => AddEditDocumentModel.Id)" @bind-Value="AddEditDocumentModel.Id" Label="@_localizer["Id"]" />
                    </MudItem>
                }
                <MudItem xs="12" md="6">
                    <MudTextField For="@(() => AddEditDocumentModel.Title)" @bind-Value="AddEditDocumentModel.Title" Label="@_localizer["Title"]" />
                </MudItem>
                <MudItem xs="12" md="6">
                    <MudTextField For="@(() => AddEditDocumentModel.Description)" @bind-Value="AddEditDocumentModel.Description" Label="@_localizer["Description"]" />
                </MudItem>
                <MudItem xs="12" md="6">
                    <MudAutocomplete T="int" Label="@_localizer["Document Type"]" For="@(() => AddEditDocumentModel.DocumentTypeId)" @bind-Value="AddEditDocumentModel.DocumentTypeId" ResetValueOnEmptyText="true" SearchFunc="@SearchDocumentTypes" Variant="Variant.Filled" ToStringFunc="@(i => _documentTypes.FirstOrDefault(b => b.Id == i)?.Name ?? string.Empty)" OffsetY="true" />
                </MudItem>
                <MudItem xs="12" md="6">
                    <MudCheckBox For="@(() => AddEditDocumentModel.IsPublic)" @bind-Checked="AddEditDocumentModel.IsPublic" Label="@_localizer["Is Public?"]" />
                </MudItem>
                <MudItem xs="12" md="6">
                    <MudItem>
                        @if (_file != null)
                        {
                            <MudAlert>@_file.Name</MudAlert>
                        }
                        @if (AddEditDocumentModel.Id != 0 && !string.IsNullOrEmpty(AddEditDocumentModel.URL))
                        {
                            <MudAlert>@(AddEditDocumentModel.URL)</MudAlert>
                        }
                        <InputFile id="fileInput" hidden OnChange="UploadFiles" />
                        <div>
                            @if (AddEditDocumentModel.Id == 0)
                            {
                                <MudButton HtmlTag="label"
                                           Variant="Variant.Text"
                                           Color="Color.Primary"
                                           Size="Size.Small"
                                           StartIcon="@Icons.Filled.CloudUpload"
                                           for="fileInput">
                                    @_localizer["Upload"]
                                </MudButton>
                            }
                            else if (!string.IsNullOrEmpty(AddEditDocumentModel.URL))
                            {
                                <MudButton Variant="Variant.Text"
                                           Color="Color.Secondary"
                                           StartIcon="@Icons.Filled.CloudDownload"
                                           Size="Size.Small"
                                           Link="@(AddEditDocumentModel.URL)" Target="_blank">
                                    @_localizer["Download"]
                                </MudButton>

                            }
                        </div>
                    </MudItem>
                </MudItem>
            </MudGrid>
        </DialogContent>
        <DialogActions>
            <MudButton Variant="Variant.Filled" OnClick="Cancel">@_localizer["Cancel"]</MudButton>
            @if (AddEditDocumentModel.Id != 0)
            {
                <MudButton Variant="Variant.Filled" ButtonType="ButtonType.Submit" Disabled="@(!Validated)" Color="Color.Secondary">@_localizer["Update"]</MudButton>
            }
            else
            {
                <MudButton Variant="Variant.Filled" ButtonType="ButtonType.Submit" Disabled="@(!Validated)" Color="Color.Success">@_localizer["Save"]</MudButton>
            }
        </DialogActions>
    </MudDialog>
</EditForm>